package coupon

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	"OnlineRetailers/models/User/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"fmt"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetCouponById struct {
	beego.Controller
}

func (c *GetCouponById) GetCouponById() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Coupon/GetCouponById"
	res := resMes.GetCoupon{}
	coupon := []*Admin.Coupon{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")

	sql := "select * from `coupon` where `coupon_id`=?"
	num, err := o.Raw(sql, id).QueryRows(&coupon)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	} else if num == 0 {
		c.Data["json"] = errorDeal.ErrMess(log, "400", fmt.Errorf(" num == 0"), addr, " num == 0")
		return
	}
	res.Coupon = coupon
	res.Code = "200"
	c.Data["json"] = res
}
